package com.apanal.qlife.common.taglib;

import java.util.List;

import org.apache.shiro.session.Session;
import org.springframework.util.StringUtils;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.dictionary.DictionaryMap;
import com.apanal.qlife.common.factory.SpringContextHolder;
import com.apanal.qlife.common.model.ModelConvert;
import com.apanal.qlife.common.shiro.ShiroHelper;
import com.apanal.qlife.sys.model.Dictionary;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.GroupService;
import com.apanal.qlife.sys.service.RoleService;
import com.apanal.qlife.sys.service.UserService;

/**
 * 公共taglib标签类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-8-5下午4:47:46
 */
public class Functions {

	private static UserService userService = SpringContextHolder.getBean("userServiceImpl");

	private static RoleService roleService = SpringContextHolder.getBean("roleServiceImpl");

	private static GroupService groupService = SpringContextHolder.getBean("groupServiceImpl");

	/**
	 * 根据类型和键获取中文字典map对应的值
	 * 
	 * @param key
	 * @param type
	 * @return
	 */
	public static String keyToValue(Integer key, String type) {
		return DictionaryMap.getValue(type, key);
	}

	/**
	 * 根据类型获取中文字典集合
	 * 
	 * @param type
	 * @return
	 */
	public static List<Dictionary> getDicList(String type) {
		return DictionaryMap.getList(type);
	}

	/**
	 * 根据用户id获取用户真实姓名,已实现缓存相应功能
	 * 
	 * @param id
	 * @return
	 */
	public static String getUserRealNameById(Long id) {
		return userService.selectUserRealNameById(id);
	}

	/**
	 * 获取当前登录用户真实姓名,已实现缓存相应功能
	 * 
	 * @param id
	 * @return
	 */
	public static String getCurrentUserRealName() {
		return userService.getCurrentUser() == null ? null : userService
				.getCurrentUser().getUseRealName();
	}

	/**
	 * 获取当前登录用户角色名字符串,多个逗号分隔,有"停用"标识
	 * 
	 * @param id
	 * @return
	 */
	public static String getCurrentUserRoleName() {
		Long userId = userService.getCurrentUserId();
		if (userId == null) {
			return null;
		}
		String result = ModelConvert
				.getNames(roleService.selectByUseId(userId));
		return StringUtils.isEmpty(result) ? "无" : result;
	}

	/**
	 * 获取当前登录用户群组名字符串,多个逗号分隔,有"停用"标识
	 * 
	 * @param id
	 * @return
	 */
	public static String getCurrentUserGroupName() {
		User user = userService.getCurrentUser();
		if (user == null) {
			return null;
		}
		String result = ModelConvert.getNames(groupService.selectByUseId(user
				.getUseId()));
		return StringUtils.isEmpty(result) ? "无" : result;
	}

	/**
	 * 根据Session获取登录用户身份(本系统是用户名)
	 * 
	 * @param session
	 * @return
	 */
	public static String principal(Session session) {
		String principal = ShiroHelper.getPrincipal(session);
		if (principal == null) {
			principal = Constants.ANONYMOUS_USER;
		}
		return principal;
	}

	/**
	 * 根据Session获取登录用户真实名
	 * 
	 * @param session
	 * @return
	 */
	public static String getUserRealNameBySession(Session session) {
		String username = principal(session);
		if (StringUtils.isEmpty(username)) {
			return null;
		}
		if (Constants.ANONYMOUS_USER.equals(username)) {
			return username;
		}
		return userService.findByUsername(username).getUseRealName();
	}

	/**
	 * 根据Session判断用户是否强制退出
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isForceLogout(Session session) {
		return session.getAttribute(Constants.SESSION_FORCE_LOGOUT_KEY) != null;
	}

	/**
	 * 根据Session得到状态
	 * 
	 * @param session
	 * @return
	 */
	public static String getSessionStatus(Session session) {
		if (session.getAttribute(Constants.SESSION_FORCE_LOGOUT_KEY) != null) {
			return "强制退出";
		}
		if (session.getAttribute(Constants.SESSION_REPEAT_LOGIN_KEY) != null) {
			return "重复登录";
		}
		if (session.getAttribute(Constants.SESSION_USER_LOCKED_KEY) != null) {
			return "账号被锁";
		}
		if (session.getAttribute(Constants.SESSION_ORG_DISABLE_KEY) != null) {
			return "机构停用";
		}
		return "在线";
	}
}
